package com.example.primarycalculator2;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.ShapeBadgeItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;

import org.litepal.LitePal;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,BottomNavigationBar.OnTabSelectedListener {
    EditText num;
    EditText time;


    BottomNavigationBar bottomNavigationBar;
    @Nullable
    TextBadgeItem numberBadgeItem;
    @Nullable
    ShapeBadgeItem shapeBadgeItem;
    int lastSelectedPosition = 0;


    public static final String TAG="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: ");

//        检测手机上有没有这个软件，如果没有才需要创建数据库
        SharedPreferences sharedPreferences=getSharedPreferences("menta", Activity.MODE_PRIVATE);
        String has=sharedPreferences.getString("has","0");
        if(has.equals("0")){
            LitePal.getDatabase();
            SharedPreferences.Editor editor= sharedPreferences.edit();
            editor.putString("has","1");
            editor.apply();;
        }


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationBar = findViewById(R.id.bottom_navigation_bar_main);
        bottomNavigationBar.setTabSelectedListener(this);
        refresh();
    }



    @SuppressLint("WrongConstant")
    private void refresh() {

        bottomNavigationBar.clearAll();
        numberBadgeItem = new TextBadgeItem()
                .setBorderWidth(4)
                .setBackgroundColorResource(R.color.blue)
                .setText("" + lastSelectedPosition)
                .setHideOnSelect(false);
        shapeBadgeItem = new ShapeBadgeItem()
                .setShape(5)
                .setShapeColorResource(R.color.teal)
                .setGravity(Gravity.TOP | Gravity.END)
                .setHideOnSelect(false);
        numberBadgeItem.toggle();
        shapeBadgeItem.toggle();
        bottomNavigationBar.setMode(2);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "home").setActiveColorResource(R.color.orange).setBadgeItem(numberBadgeItem))
                .addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "error book").setActiveColorResource(R.color.teal))
                .addItem(new BottomNavigationItem(R.drawable.ic_github_circle_white_24dp, "user").setActiveColorResource(R.color.blue).setBadgeItem(shapeBadgeItem))
                .setFirstSelectedPosition(lastSelectedPosition > 2 ? 2 : lastSelectedPosition)
                .initialise();
    }





    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.start){
//            检查数据
            num=findViewById(R.id.num);
            time=findViewById(R.id.time);
            String num_s=num.getText().toString();
            String time_s=time.getText().toString();
            if(num_s.equals("")||time_s.equals("")){
                Toast.makeText(this,"请填写完整",Toast.LENGTH_SHORT).show();
                return;
            }
            int num_i=Integer.valueOf(num_s);
            int time_i=Integer.valueOf(time_s);

            Intent intent=new Intent(MainActivity.this,HomeworkActivity.class);
            intent.putExtra("minutes",time_i);
            intent.putExtra("num_problems",num_i);
//            Log.d("MainActivity : 时间 ",String.valueOf(time_i));
            startActivity(intent);

        }


    }

    @Override
    public void onTabSelected(int position) {
        lastSelectedPosition = position;
        Log.d(TAG, "onTabSelected: "+lastSelectedPosition);
        switch (lastSelectedPosition){
            case 0:
                break;
            case 1:
                Intent intent1=new Intent(MainActivity.this,ErrorActivity.class);
                startActivity(intent1);
                break;
            case 2:
                Intent intent2=new Intent(MainActivity.this,UserActivity.class);
                startActivity(intent2);
                break;
        }

    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}